__author__ = 'sdjkx'
try:
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
except ImportError:
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *

from libs.utils import newIcon, labelValidator

BB = QDialogButtonBox

class ClassDialog(QDialog):
    selectItemList = []

    def __init__(self, parent=None, listItem=None):
        super(ClassDialog, self).__init__(parent)

        self.listItem = listItem
        self.setWindowTitle("选择类型(可多选)")

        self.layout = QVBoxLayout()
        self.buttonBox = bb = BB(BB.Ok | BB.Cancel, Qt.Horizontal, self)
        bb.button(BB.Ok).setIcon(newIcon('done'))
        bb.button(BB.Cancel).setIcon(newIcon('undo'))
        bb.accepted.connect(self.validate)
        bb.rejected.connect(self.reject)
        self.layout.addWidget(bb)

        self.label = QLabel()
        self.label.setText("按住CTRL可多选")
        self.layout.addWidget(self.label)

        self.listWidget = QListWidget(self)
        self.listWidget.setSelectionMode(QAbstractItemView.ExtendedSelection) #按住CTRL可多选
        self.setListWidget()
        self.layout.addWidget(self.listWidget)

        self.setLayout(self.layout)

    def setListWidget(self):
        if self.listItem is not None and len(self.listItem) > 0:
            for item in self.listItem:
                self.listWidget.addItem(item)

            # self.listWidget.itemClicked.connect(self.listItemClick)
            self.listWidget.itemDoubleClicked.connect(self.listItemDoubleClick)

    def listItemDoubleClick(self, tQListWidgetItem):
        # self.listItemClick(tQListWidgetItem)
        self.validate()

    def validate(self):
        text_list = self.listWidget.selectedItems()
        if len(text_list) > 0:
            for item in text_list:
                ClassDialog.selectItemList.append(item.text())
            self.accept()

    @staticmethod
    def getClassItemList(parent=None, listItem=None):
        ClassDialog.selectItemList = []
        dialog = ClassDialog(parent, listItem)
        dialog.exec_()

        return dialog.selectItemList
